package com.cy.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cy.annotation.SysLog;
import com.cy.service.MemberService;
import com.cy.util.ExportExcel;
import com.cy.vo.TeamVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.entity.Team;
import com.cy.service.TeamService;
import com.cy.utils.PageUtils;
import com.cy.utils.Query;
import com.cy.utils.R;


/**
 * 
 * 
 * @author chenyi
 * @email qq228112142@qq.com
 * @date 2017-08-08 17:36:01
 */
@Controller
@RequestMapping("team")
public class TeamController {
	@Autowired
	private TeamService teamService;
	@Autowired
	private MemberService memberService;
	
	/**
	 * 列表
	 */
    @ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("team:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<Team> teamList = teamService.queryList(query);
		int total = teamService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(teamList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 导出
	 */
	@ResponseBody
	@RequestMapping("/export")
	@SysLog("导出团队列表")
	@RequiresPermissions("team:list")
	public R export(@RequestParam Map<String, Object> params, HttpServletResponse response){

		ExportExcel<TeamVo> ex = new ExportExcel<TeamVo>();
		String[] headers =
				{ "团队码", "发起人云支付账号", "发起人姓名", "发起人电话号码", "发起时间","团队人数"};
		List<TeamVo> data = teamService.exportList();
		try
		{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String fileName="团队列表"+sdf.format(new Date())+".xls";
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
			ServletOutputStream outputStream = response.getOutputStream();
			ex.exportExcel(fileName,headers, data, outputStream,"yyyy-MM-dd HH:mm:ss");
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.ok();
	}
    /**
     * 跳转到新增页面
     **/
    @RequestMapping("/add")
    @RequiresPermissions("team:save")
    public String add(){
        return "team/add.jsp";
    }

    /**
     *   跳转到修改页面
     **/
    @RequestMapping("/edit/{id}")
    @RequiresPermissions("team:update")
    public String edit(HttpServletRequest request, Model model, @PathVariable("id") Long id){
			Team team = teamService.queryObject(id);
        model.addAttribute("model", team);
        return "team/edit.jsp";
    }

	/**
	 *   跳转到详情页面
	 **/
	@RequestMapping("/detail/{teamCode}")
	public String detail(HttpServletRequest request, Model model, @PathVariable("teamCode") String teamCode){
		model.addAttribute("teamCode",teamCode);
		return "team/detail.jsp";
	}


	/**
	 * 信息
	 */
    @ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("team:info")
	public R info(@PathVariable("id") Long id){
		Team team = teamService.queryObject(id);
		
		return R.ok().put("team", team);
	}
	
	/**
	 * 保存
	 */
    @ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("team:save")
	public R save(@RequestBody Team team){
		teamService.save(team);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
    @ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("team:update")
	public R update(@RequestBody Team team){
		teamService.update(team);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
    @ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("team:delete")
	public R delete(@RequestBody Long[] ids){
		teamService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
